package com.rongshe.web.controller.hotel;

import com.alibaba.fastjson2.JSONObject;
import com.rongshe.common.core.controller.BaseController;
import com.rongshe.common.core.domain.AjaxResult;
import com.rongshe.common.core.domain.entity.SysUser;
import com.rongshe.common.exception.ServiceException;
import com.rongshe.common.utils.SecurityUtils;
import com.rongshe.common.utils.StringUtils;
import com.rongshe.framework.web.service.SysLoginService;
import com.rongshe.hotel.domain.RsUser;
import com.rongshe.hotel.domain.WxLoginDTO;
import com.rongshe.hotel.service.IRsUserService;
import com.rongshe.hotel.utils.WxUtils;
import com.rongshe.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/hotel/wx")
public class RsWxController  extends BaseController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private IRsUserService rsUserService;

    //获取手机号
    @Autowired
    private WxUtils wxUtils;

    @PostMapping("/login")
    public AjaxResult wxPhoneLogin(@RequestBody WxLoginDTO dto) {
        HashMap<String, Object> loginResult = new HashMap<>();
        // 1. 获取session_key和openid
        JSONObject sessionInfo = wxUtils.getSessionKey(dto.getCode());
        String sessionKey = sessionInfo.getString("session_key");
        String openId = sessionInfo.getString("openid");
        String unionid = sessionInfo.getString("unionid");

        if (StringUtils.isEmpty(sessionKey) || StringUtils.isEmpty(openId)) {
            throw new ServiceException("获取微信信息失败");
        }
        RsUser rsUser = new RsUser();
        rsUser.setOpenId(openId);
        rsUser.setUnionId(unionid);
        List<RsUser> rsUsers = rsUserService.selectRsUserList(rsUser);
        if(StringUtils.isEmpty(dto.getEncryptedData())){
            loginResult.put("needPhone",true);
            if(rsUsers.size() ==0 ){
                // 创建用户
                SysUser sysUser = new SysUser();
                sysUser.setUserName(openId);
                sysUser.setNickName(openId);
                sysUser.setPassword(SecurityUtils.encryptPassword(openId));
                sysUser.setUserType("01");
                userService.registerUser(sysUser);
                rsUser.setUserId(sysUser.getUserId());
                rsUser.setAdmin("N");
                rsUserService.insertRsUser(rsUser);
            }else{
                RsUser existRsUser = rsUsers.get(0);
                if(StringUtils.isNotEmpty(existRsUser.getUserPhone())){
                    loginResult.put("needPhone",false);
                }
            }
        }else{
            // 2. 解密手机号
            String phone = wxUtils.decryptPhone(sessionKey, dto.getEncryptedData(), dto.getIv());
            // 更新用户信息
            RsUser existRsUser = rsUsers.get(0);
            existRsUser.setUserPhone(phone);
            rsUserService.updateRsUser(existRsUser);
            SysUser sysUser = new SysUser();
            sysUser.setUserId(existRsUser.getUserId());
            sysUser.setPhonenumber(phone);
            userService.updateUser(sysUser);
            loginResult.put("needPhone",false);
        }
        //用户登录
        String token = loginService.loginByWX(openId, openId);
        List<RsUser> rsUserExistList = rsUserService.selectRsUserList(rsUser);
        RsUser existRsUser = rsUserExistList.get(0);
        loginResult.put("userInfo",existRsUser);
        loginResult.put("token",token);
        return AjaxResult.success(loginResult);
    }

}
